/*******************************************************************************
 *  Copyright (c) 2010 Chase Technology Ltd.
 *  
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the GNU General Public License v3.0
 *  which is available at http://www.gnu.org/licenses/
 * 
 * Contributors:
 *     Doug Satchwell - initial API and implementation
 *******************************************************************************/
package org.jfunction.internal.parser;

import org.eclipse.jface.text.rules.EndOfLineRule;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IWordDetector;
import org.eclipse.jface.text.rules.PatternRule;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.WordRule;

/**
 * Scanner for the scenario sections of a scenario file
 * 
 * @author Doug Satchwell
 */
public class ScenarioScanner extends RuleBasedScanner {

	public ScenarioScanner(final KeyWords keywords) {
		PatternRule scenarioRule = new PatternRule(keywords.scenario(),":",JFunctionToken.SCENARIO,(char)0,false);
		scenarioRule.setColumnConstraint(0);
		
		EndOfLineRule commentRule = new EndOfLineRule(keywords.comment(), JFunctionToken.COMMENT);
		
//		EndOfLineRule stepRule = new EndOfLineRule(keywords.step(), JFunctionToken.STEP, '\\', true);

		PatternRule fieldRule = new PatternRule("<",">",JFunctionToken.FIELD,(char)0,false,false);
		
		WordRule stepRule = new WordRule(new IWordDetector() {

			public boolean isWordStart(char c) {
				return c == '-';
			}

			public boolean isWordPart(char c) {
				return false;
			}
		});
		stepRule.addWord("-", JFunctionToken.STEP);
		stepRule.setColumnConstraint(0);

		WordRule eolRule = new WordRule(new IWordDetector() {

			public boolean isWordStart(char c) {
				return c == '\r' || c == '\n';
			}

			public boolean isWordPart(char c) {
				return c == '\n';
			}
		});
		eolRule.addWord("\r\n", JFunctionToken.EOL);
		eolRule.addWord("\n", JFunctionToken.EOL);

		IRule[] rules = new IRule[] {
			commentRule, 
			scenarioRule,
			stepRule,
			fieldRule,
//			eolRule
		};
		setRules(rules);
	}
}
